package configurationBusiness;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/10/23.
 * 项目管理 对configuration的type=project 做管理
 */
public class ProjectManager implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        ReturnJson returnJson = new ReturnJson();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        JSONObject requestInfo = GetRequestInputStream.getJson(request);

        String action = requestInfo.getString("action");

        if (Objects.equals("getProject", action)){
            String sql = "SELECT content FROM configuration WHERE type='project' AND `status`='1' ORDER BY createtime DESC";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(sql);
            JSONArray projectList = new JSONArray();
            if (sqlReturnModel.getIsSuccess()){
                for (ArrayList<String> a: sqlReturnModel.getSelectReturnData()){
                    projectList.add(a.get(0));
                }
                returnJson.setResult(projectList);
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("项目列表查询失败：" + sqlReturnModel.getFailingReason());
            }
        }
        if (Objects.equals("addProject", action)){
            String projectName = requestInfo.getString("projectName");
            //判断是否存在
            String selectSql = "SELECT content FROM `configuration` WHERE content=\"" + projectName + "\" AND `status`=\"1\"";
            SqlReturnModel sqlReturnModel = sqlDispatch.selectWord(selectSql);
            if (sqlReturnModel.getSelectReturnData().size() != 0){
                returnJson.setResult("项目已经存在，名字相同的不要啊！");
                return returnJson.toString();
            }

            String sql = "INSERT INTO `configuration` (`type`, `content`, `status`) VALUES ('project', '" + projectName + "', '1')";
            sqlReturnModel = sqlDispatch.insert(sql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setResult("添加成功");
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("添加项目失败：" + sqlReturnModel.getFailingReason());
            }
        }
        if (Objects.equals("delProject", action)){
            String projectName = requestInfo.getString("projectName");
            String sql = "UPDATE `configuration` SET `status`='0' WHERE (`content`='" + projectName + "')";
            SqlReturnModel sqlReturnModel = sqlDispatch.update(sql);
            if (sqlReturnModel.getIsSuccess()){
                returnJson.setResult("删除成功");
            }else {
                returnJson.setApiStatus(0);
                returnJson.setErrorMsg("移除项目失败：" + sqlReturnModel.getFailingReason());
            }
        }

        return returnJson.toString();
    }
}
